######################
# Replication CES, CMPS, VOTER Survey Models & Appendix Tables.
# "Embracing the Status Hierarchy: How Immigration Attitudes, Prejudice, and
# Sexism Shaped Non-White Support for Trump”
# Geiger and Reny 2024
# Perspectives in Politics
# For questions please contact Jessica Geiger
# jessica.geiger@cgu.edu
######################

rm(list=ls())
pacman::p_load(haven, tidyverse, lubridate, 
               stargazer, estimatr, simcf)
# simcf is not on CRAN and needs to be downloaded  
# though analyses could be closely replicated using another package like
# marginaleffects on CRAN

setwd('~/replication')


######################
# CCES 
######################

# Appendix Tables C6 and C7 - CCES 2020

cces <- read_csv("/data/cces_replication.csv")

# Immigration policy 
summary(m1 <- glm(trump_hr ~ imm_policy + college_educ + income_under_60 + income_missing + female + age + pid7 + ideo5, data=cces[cces$white==1,],family=binomial(link='logit')))
summary(m2 <- glm(trump_hr ~ imm_policy + college_educ + income_under_60 + income_missing + female + age + pid7 + ideo5, data=cces[cces$black==1,],family=binomial(link='logit')))
summary(m3 <- glm(trump_hr ~ imm_policy + college_educ + income_under_60 + income_missing + female + age + pid7 + ideo5 + indian + aapi_other + korean, cces[cces$asian==1,],family=binomial(link='logit')))
summary(m4 <- glm(trump_hr ~ imm_policy + college_educ + income_under_60 + income_missing + female + age + pid7 + ideo5 + cuban + puertorican + hispanic_other, data=cces[cces$latino==1,],family=binomial(link='logit')))

# Racial Resentment
summary(m5 <- glm(trump_hr ~ rr + college_educ + income_under_60 + income_missing + female + age + pid7 +ideo5, data=cces[cces$white==1,],family=binomial(link='logit')))
summary(m6 <- glm(trump_hr ~ rr + college_educ + income_under_60 + income_missing + female + age + pid7 +ideo5, data=cces[cces$black==1,],family=binomial(link='logit')))
summary(m7 <- glm(trump_hr ~ rr + college_educ + income_under_60 + income_missing + female + age + pid7 + ideo5 + indian + aapi_other + korean, cces[cces$asian==1,],family=binomial(link='logit')))
summary(m8 <- glm(trump_hr ~ rr + college_educ + income_under_60 + income_missing + female + age + pid7 +ideo5 + cuban + puertorican + hispanic_other, data=cces[cces$latino==1,],family=binomial(link='logit')))

stargazer(m1, m2, m3, m4, m5, m6, m7, m8,
          type="text",
          out='tables/c6.tex',
          omit.stat = c('ll','aic'),
          dep.var.labels = "",
          column.labels = c("White","Black", "AAPI","Latino",
                            "White","Black", "AAPI","Latino", 
                            "White","Black", "AAPI","Latino"),
          model.names = F,
          title = "Logistic Regression Models Predicting Trump Vote",
          covariate.labels = c("Immigration Policy","Racial Resentment",
                               "College Education","Household Income",
                               "Household Income Missing", "Gender", "Age",
                               "Party ID", "Ideology", 
                               "Indian", "Other AAPI", "Korean",
                               "Cuban", "Puerto Rican", "Hispanic Other",
                                "Constant")
          )

# Sexism
summary(m13 <- glm(trump_hr ~ sexism + college_educ + income_under_60+ income_missing + female + age + pid7 + ideo5, cces[cces$white==1,],family=binomial(link='logit')))
summary(m14 <- glm(trump_hr ~ sexism + college_educ + income_under_60+ income_missing + female + age + pid7 + ideo5, cces[cces$black==1,],family=binomial(link='logit')))
summary(m15 <- glm(trump_hr ~ sexism + college_educ + income_under_60+ income_missing + female + age + pid7 + ideo5 + indian + aapi_other + korean, cces[cces$asian==1,],family=binomial(link='logit')))
summary(m16 <- glm(trump_hr ~ sexism + college_educ + income_under_60+ income_missing + female + age + pid7 + ideo5 + cuban + puertorican + hispanic_other, cces[cces$latino==1,],family=binomial(link='logit')))

stargazer(m13, m14, m15, m16,
          type="text",
          out='tables/c7.tex',
          omit.stat = c('ll','aic'),
          dep.var.labels = "",
          column.labels = c("White","Black", "AAPI","Latino"),
          model.names = F,
          title = "Logistic Regression Models Predicting Trump Vote",
          covariate.labels = c("Sexism","College Education","Household Income",
                               "Household Income Missing","Gender", "Age", 
                               "Party ID", "Ideology", 
                               "Indian", "Other AAPI", "Korean",
                               "Cuban", "Puerto Rican", "Hispanic Other",
                                "Constant")
          )

# Appendix Tables C8 - CCES 2016
cces2016 <- read_csv("data/cces2016_replication.csv")

#Immigration policy 
summary(m1 <- glm(trump_hr ~ imm_policy + college_educ + income_under_60 + income_missing + female + age + pid7 + ideo5, data=cces2016[cces2016$white==1,],family=binomial(link='logit')))
summary(m2 <- glm(trump_hr ~ imm_policy + college_educ + income_under_60 + income_missing + female + age + pid7 + ideo5, data=cces2016[cces2016$black==1,],family=binomial(link='logit')))
summary(m3 <- glm(trump_hr ~ imm_policy + college_educ + income_under_60 + income_missing + female + age + pid7 + ideo5 + indian + aapi_other + korean, cces2016[cces2016$asian==1,],family=binomial(link='logit')))
summary(m4 <- glm(trump_hr ~ imm_policy + college_educ + income_under_60 + income_missing + female + age + pid7 + ideo5+ cuban + puertorican + hispanic_other, data=cces2016[cces2016$latino==1,],family=binomial(link='logit')))

# FIRE Scale
summary(m5 <- glm(trump_hr ~ rr +college_educ + income_under_60+ income_missing + female + age + pid7 + ideo5, data=cces2016[cces2016$white==1,],family=binomial(link='logit')))
summary(m6 <- glm(trump_hr ~ rr +college_educ + income_under_60+ income_missing + female + age + pid7 + ideo5, data=cces2016[cces2016$black==1,],family=binomial(link='logit')))
summary(m7 <- glm(trump_hr ~ rr + college_educ + income_under_60+ income_missing + female + age + pid7 + ideo5 + indian + aapi_other + korean, cces2016[cces2016$asian==1,],family=binomial(link='logit')))
summary(m8 <- glm(trump_hr ~ rr +college_educ + income_under_60+ income_missing + female + age + pid7 + ideo5 + cuban + puertorican + hispanic_other, data=cces2016[cces2016$latino==1,],family=binomial(link='logit')))

stargazer(m1, m2, m3, m4, m5, m6, m7, m8,
          type="text",
          out='tables/c8.tex',
          omit.stat = c('ll','aic'),
          dep.var.labels = "",
          column.labels = c("White","Black", "AAPI","Latino", 
                            "White","Black", "AAPI","Latino"),
          model.names = F,
          title = "Logistic Regression Models Predicting Trump Vote 2016",
          covariate.labels = c("Immigration Policy","FIRE Scale","College Education",
                               "Household Income","Household Income Missing", 
                               "Gender", "Age", "Party ID", "Ideology", 
                               "Indian", "Other AAPI", "Korean",
                               "Cuban", "Puerto Rican", "Hispanic Other", 
                                "Constant")
          )

######################
# CMPS 
######################

# Appendix Tables C9 and C10 - CMPS 2020

cmps <- read_csv("data/cmps_replication.csv")

# Immigration policy models
summary(m1 <- glm(trump_hr ~ imm_policy + college_educ + household_income + female + age + pid7 + ideo5, data=cmps[cmps$race==1,],family=binomial(link='logit')))
summary(m2 <- glm(trump_hr ~ imm_policy + college_educ + household_income + female + age + pid7 + ideo5, data=cmps[cmps$race==3,],family=binomial(link='logit')))
summary(m3 <- glm(trump_hr ~ imm_policy + college_educ + household_income + female + age + pid7 + ideo5 + indian + aapi_other + korean, cmps[cmps$race==4,],family=binomial(link='logit')))
summary(m4 <- glm(trump_hr ~ imm_policy + college_educ + household_income + female + age + pid7 + ideo5 + cuban + puertorican + hispanic_other, data=cmps[cmps$race==2,],family=binomial(link='logit')))

# Racial Resentment models
summary(m5 <- glm(trump_hr ~ rr + college_educ + household_income + female + age + pid7 + ideo5, data=cmps[cmps$race==1,],family=binomial(link='logit')))
summary(m6 <- glm(trump_hr ~ rr + college_educ + household_income + female + age + pid7 + ideo5, data=cmps[cmps$race==3,],family=binomial(link='logit')))
summary(m7 <- glm(trump_hr ~ rr + college_educ + household_income + female + age + pid7 + ideo5 + indian + aapi_other + korean, cmps[cmps$race==4,],family=binomial(link='logit')))
summary(m8 <- glm(trump_hr ~ rr + college_educ + household_income + female + age + pid7 + ideo5 + cuban + puertorican + hispanic_other, data=cmps[cmps$race==2,],family=binomial(link='logit')))

stargazer(m1, m2, m3, m4, m5, m6, m7, m8,
          type="text",
          out='tables/c9.tex',
          omit.stat = c('ll','aic'),
          dep.var.labels = "",
          column.labels = c("White","Black", "AAPI","Latino", 
                            "White","Black", "AAPI","Latino"),
          model.names = F,
          title = "Logistic Regression Models Predicting Trump Vote",
          covariate.labels = c("Immigration Policy","Racial Resentment",
                               "College Education","Household Income",
                               "Gender", "Age", "Party ID", "Ideology", 
                               "Indian", "Other AAPI", "Korean",
                               "Cuban", "Puerto Rican", "Hispanic Other", 
                               "Constant"))

# White Ethnocentrism models
summary(m9 <- glm(trump_hr ~ ethnocentrism2 +college_educ + household_income + female + age + pid7 +ideo5, data=cmps[cmps$race==1,],family=binomial(link='logit')))
summary(m10 <- glm(trump_hr ~ ethnocentrism2 +college_educ + household_income + female + age + pid7 +ideo5, data=cmps[cmps$race==3,],family=binomial(link='logit')))
summary(m11 <- glm(trump_hr ~ ethnocentrism2 + college_educ + household_income + female + age + pid7 + ideo5 + indian + aapi_other + korean, cmps[cmps$race==4,],family=binomial(link='logit')))
summary(m12 <- glm(trump_hr ~ ethnocentrism2 +college_educ + household_income + female + age + pid7 +ideo5 + cuban + puertorican + hispanic_other, data=cmps[cmps$race==2,],family=binomial(link='logit')))

# Sexism models
summary(m13 <- glm(trump_hr ~ sexism + college_educ + household_income + female + age + pid7 + ideo5, cmps[cmps$race==1,],family=binomial(link='logit')))
summary(m14 <- glm(trump_hr ~ sexism + college_educ + household_income + female + age + pid7 + ideo5, cmps[cmps$race==3,],family=binomial(link='logit')))
summary(m15 <- glm(trump_hr ~ sexism + college_educ + household_income + female + age + pid7 + ideo5 + indian + aapi_other + korean, cmps[cmps$race==4,],family=binomial(link='logit')))
summary(m16 <- glm(trump_hr ~ sexism + college_educ + household_income + female + age + pid7 + ideo5 + cuban + puertorican + hispanic_other, cmps[cmps$race==2,],family=binomial(link='logit')))

stargazer(m9, m10, m11, m12, m13, m14, m15, m16,
          type="text",
          out='tables/c10.tex',
          omit.stat = c('ll','aic'),
          dep.var.labels = "",
          column.labels = c("White","Black", "AAPI","Latino",
                            "White","Black", "AAPI","Latino"),
          model.names = F,
          title = "Logistic Regression Models Predicting Trump Vote",
          covariate.labels = c("White Ethnocentrism","Sexism","College Education",
                               "Household Income","Gender", "Age",
                               "Party ID", "Ideology",
                               "Indian", "Other AAPI", "Korean",
                               "Cuban", "Puerto Rican", "Hispanic Other", 
                               "Constant"))


######################
# VOTER Survey 
######################

# Appendix Tables C17, C18, and C19 - VOTER Survey 2011 and 2016
voter <- read_csv("data/voter_replication.csv")

#Immigration policy 
summary(m1 <- lm(vote_romn_2012 ~ imm_policy +college + faminc + female + age + pid7 +ideo5, data=voter[voter$white==1,]))
summary(m2 <- lm(vote_trump_2016 ~ imm_policy +college + faminc + female + age + pid7 +ideo5, data=voter[voter$white==1,]))
summary(m3 <- lm(vote_romn_2012 ~ imm_policy +college + faminc + female + age + pid7 +ideo5, data=voter[voter$black==1,]))
summary(m4 <- lm(vote_trump_2016 ~ imm_policy +college + faminc + female + age + pid7 +ideo5, data=voter[voter$black==1,]))
summary(m5 <- lm(vote_romn_2012 ~ imm_policy +college + faminc + female + age + pid7 +ideo5, data=voter[voter$latino==1,]))
summary(m6 <- lm(vote_trump_2016 ~ imm_policy +college + faminc + female + age + pid7 +ideo5, data=voter[voter$latino==1,]))

# Racial Resentment 
summary(m7 <- lm(vote_romn_2012 ~ rr + college + faminc + female + age + pid7 + ideo5, data=voter[voter$white==1,]))
summary(m8 <- lm(vote_trump_2016 ~ rr + college + faminc + female + age + pid7 + ideo5, data=voter[voter$white==1,]))
summary(m9 <- lm(vote_romn_2012 ~ rr + college + faminc + female + age + pid7 + ideo5, data=voter[voter$black==1,]))
summary(m10 <- lm(vote_trump_2016 ~ rr + college + faminc + female + age + pid7 + ideo5, data=voter[voter$black==1,]))
summary(m11 <- lm(vote_romn_2012 ~ rr + college + faminc + female + age + pid7 + ideo5, data=voter[voter$latino==1,]))
summary(m12 <- lm(vote_trump_2016 ~ rr + college + faminc + female + age + pid7 + ideo5, data=voter[voter$latino==1,]))

#White Ethnocentrism 
summary(m13 <- lm(vote_romn_2012 ~ ethnocentrism +college + faminc + female + age + pid7 + ideo5, data=voter[voter$white==1,]))
summary(m14 <- lm(vote_trump_2016 ~ ethnocentrism +college + faminc + female + age + pid7 + ideo5, data=voter[voter$white==1,]))
summary(m15 <- lm(vote_romn_2012 ~ ethnocentrism +college + faminc + female + age + pid7 + ideo5, data=voter[voter$black==1,]))
summary(m16 <- lm(vote_trump_2016 ~ ethnocentrism +college + faminc + female + age + pid7 + ideo5, data=voter[voter$black==1,]))
summary(m17 <- lm(vote_romn_2012 ~ ethnocentrism +college + faminc + female + age + pid7 + ideo5, data=voter[voter$latino==1,]))
summary(m18 <- lm(vote_trump_2016 ~ ethnocentrism +college + faminc + female + age + pid7 + ideo5, data=voter[voter$latino==1,]))

stargazer(m1, m2, m3, m4, m5, m6, 
          type="text",
          omit.stat=c('ser','f','rsq'),
          dep.var.labels = c("Romney 12", "Trump 16", 
                             "Romney 12", "Trump 16", 
                             "Romney 12", "Trump 16"),
          column.labels = c("White","White", "Black","Black", 
                            "Latino", "Latino"),
          model.names = F,
          title = "Linear Probability Models Predicting 2016 Trump & 2012 Romney Vote",
          covariate.labels = c("Immigration Policy","College Education","Household Income",
                               "Gender", "Age", "Party ID", "Ideology", "Constant"),
          dep.var.caption = "DV: Trump Vote",
          se = starprep(m1, m2, m3, m4, m5, m6),
          out = 'tables/c17.tex')

stargazer( m7, m8, m9, m10, m11, m12,   
           type="text",
           omit.stat=c('ser','f','rsq'),
           dep.var.labels = "",
           column.labels = c("White","White", 
                             "Black","Black", 
                             "Latino", "Latino"),
           model.names = F,
           title = "Linear Probability Models Predicting 2016 Trump & 2012 Romney Vote",
           covariate.labels = c("Racial Resentment","College Education",
                                "Household Income","Gender", "Age", "Party ID",
                                "Ideology", "Constant"),
           dep.var.caption = "DV: Trump Vote",
           se = starprep(m7, m8, m9, m10, m11, m12),
           out = 'tables/c18.tex')

stargazer( m13, m14, m15, m16, m17, m18,   
           type="text",
           omit.stat=c('ser','f','rsq'),
           dep.var.labels = "",
           column.labels = c("White","White", 
                             "Black","Black", 
                             "Latino", "Latino"),
           model.names = F,
           title = "Linear Probability Models Predicting 2016 Trump & 2012 Romney Vote",
           covariate.labels = c("White Ethnocentrism","College Education","Household Income",
                                "Gender", "Age", "Party ID", "Ideology", "Constant"),
           dep.var.caption = "DV: Trump Vote",
           se = starprep(m13, m14, m15, m16, m17, m18),
           out = 'tables/c19.tex')
